﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.ApplicationPages;
//using Microsoft.MCSUK.SPSiteDirectory2010.Features.DeleteBrokenListingJob;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using System.Web;
namespace Microsoft.MCSUK.SPSiteDirectory2010
{
    public class DeleteBrokenListingJobManager : LayoutsPageBase
    {
        protected System.Web.UI.WebControls.Literal ErrorMessageLiteral;
        protected System.Web.UI.WebControls.Button OKButton;
        protected System.Web.UI.WebControls.DropDownList DeleteBrokenListingNumberScans;
        protected Microsoft.SharePoint.WebControls.SchedulePicker JobSchedule;
        protected System.Web.UI.WebControls.Label WebAppStatus;
        protected System.Web.UI.WebControls.Button DisableButton;
        protected override void OnLoad(EventArgs e)
        {
            GlobalMethods globalMethods = new GlobalMethods();
            GlobalSolutionSettings globalSolutionSettings = new GlobalSolutionSettings().GetOrCreateGlobalSolutionSettings(SPFarm.Local);

            if (!IsPostBack) ///we do not want to do anything on postbacks
            {
                try
                {
                    if (globalMethods.DoesSiteExist(globalSolutionSettings.SiteDirectoryURL))
                    {
                        using (SPSite siteDirectorySite = new SPSite(globalSolutionSettings.SiteDirectorySiteGuid))
                        {
                            SPWebApplication siteDirectoryWebApp = siteDirectorySite.WebApplication;
                            SPFeature jobFeature = siteDirectoryWebApp.Features[GlobalConstants.DeleteJobFeatureID]; //get the job feature status on the web application and display text accordingly
                            if (jobFeature != null)
                            {
                                WebAppStatus.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_DeleteBrokenListingJob_JobActivated").ToString();
                                JobSchedule.Schedule = siteDirectoryWebApp.JobDefinitions[globalSolutionSettings.DeleteJobDefinitionGUID].Schedule;
                                DeleteBrokenListingNumberScans.ClearSelection();
                                DeleteBrokenListingNumberScans.Items.FindByText(globalSolutionSettings.DeleteBrokenListingNumberScans.ToString()).Selected = true;
                            }
                            else
                            {
                                WebAppStatus.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_DeleteBrokenListingJob_JobNotActivated").ToString();
                            }                            
                        }
                    }
                    else //if the siteDirectoryWebApp is null then it has not yet been configured. disable the ok button and throw an error message
                    {
                        OKButton.Enabled = false;
                        DisableButton.Enabled = false;
                        ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_Generic_SiteDirectoryNotConfigured").ToString();
                    }
                }
                catch (Exception ex)
                {
                    ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_GenericUIErrorText").ToString();
                    globalMethods.WriteULSEntry(ex, TraceSeverity.Unexpected, null);
                }
            }

        }
        protected void DisableButton_OnClick(object sender, EventArgs e)
        {
            GlobalMethods globalMethods = new GlobalMethods();
            GlobalSolutionSettings globalSolutionSettings = new GlobalSolutionSettings().GetOrCreateGlobalSolutionSettings(SPFarm.Local);
            try
            {
                if (globalMethods.DoesSiteExist(globalSolutionSettings.SiteDirectoryURL))
                {
                    using (SPSite siteDirectorySite = new SPSite(globalSolutionSettings.SiteDirectorySiteGuid))
                    {
                        SPWebApplication siteDirectoryWebApp = siteDirectorySite.WebApplication; //we can only get this if the site directory is configured
                        if (globalSolutionSettings.IsDeleteBrokenListingJobConfigured)
                        {
                            globalMethods.DeactivateFeatureInWebApp(siteDirectoryWebApp, GlobalConstants.DeleteJobFeatureID);
                        }
                    }
                }
                else
                {
                    globalMethods.DeactivateFeatureInWebApp(null, GlobalConstants.DeleteJobFeatureID);
                }
                SPUtility.Redirect(GlobalConstants.CentralAdminRedirectURL, SPRedirectFlags.Trusted | SPRedirectFlags.DoNotEndResponse, this.Context);
            }
            catch (Exception ex)
            {
                ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_GenericUIErrorText").ToString();
                globalMethods.WriteULSEntry(ex, TraceSeverity.Unexpected, null);
            }
        }
        protected void OKButton_OnClick(object sender, EventArgs e)
        {
            GlobalMethods globalMethods = new GlobalMethods();
            GlobalSolutionSettings globalSolutionSettings = new GlobalSolutionSettings().GetOrCreateGlobalSolutionSettings(SPFarm.Local);
            try
            {
                if (Page.IsValid) 
                {
                    using (SPSite siteDirectorySite = new SPSite(globalSolutionSettings.SiteDirectorySiteGuid))
                    {
                        Guid jobID = globalMethods.ReActivateFeatureInWebApp(siteDirectorySite.WebApplication, GlobalConstants.DeleteJobFeatureID, GlobalConstants.DeleteBrokenListingJobName, JobSchedule.Schedule);
                        globalSolutionSettings.IsDeleteBrokenListingJobConfigured = true;
                        globalSolutionSettings.DeleteBrokenListingNumberScans = Convert.ToInt32(DeleteBrokenListingNumberScans.SelectedItem.Text);
                        globalSolutionSettings.DeleteJobDefinitionGUID = jobID;
                        globalSolutionSettings.Update();
                    }
                }
                SPUtility.Redirect(GlobalConstants.CentralAdminRedirectURL, SPRedirectFlags.Trusted | SPRedirectFlags.DoNotEndResponse, this.Context);
            }
            catch (Exception ex)
            {
                ErrorMessageLiteral.Text = HttpContext.GetGlobalResourceObject("SPSiteDirectory2010GlobalResources", "Code_GenericUIErrorText").ToString();
                globalMethods.WriteULSEntry(ex, TraceSeverity.Unexpected, null);
            }
        }
    }
}
